package com.fk.servlet.admin;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fk.dao.impl.DesignerDaoImpl;
import com.fk.entity.Designer;
import com.fk.uitl.BaseServlet;
import com.fk.uitl.Result;
import com.google.gson.Gson;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;

public class AdminRoleCategroyServlet extends BaseServlet {
	/**
	 * 查询所有设计师人员
	 * localhost:8080/wuyijiuchaoliufushiguan/adminRoleCategroy?reqName=getRoleCates
	 * 
	 **/

	public void getRoleCates(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		DesignerDaoImpl deDao = new DesignerDaoImpl();// 创建实现类对象
		List<Designer> deList = deDao.getDesignerList();// 查到所有设计师集合

		// 发送ajax请求，服务器基本都会返回json格式字符串

		response.setCharacterEncoding("UTF-8");// 设置响应的编码格式
		PrintWriter out = response.getWriter();// out对象

		Gson gson = new Gson();
		String res = gson.toJson(deList);// 将deList转化为json字符串

		out.print(res);// 响应数据

		out.flush();
		out.close();
	}

	/**
	 * localhost:8080/wuyijiuchaoliufushiguan/adminRoleCategroy?reqName=delRoleCate
	 **/

	public void delRoleCate(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int rId = Integer.parseInt(request.getParameter("designerId"));// 接受前台传来的参数
		DesignerDaoImpl deDao = new DesignerDaoImpl();
		int n = deDao.deleteRoleCateById(rId);// 调用删除的方法

		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();

		if (n > 0) {
			out.print(Result.toClient(true, "删除成功", ""));
		} else {
			out.print(Result.toClient(false, "删除失败", ""));
		}
		out.flush();
		out.close();

	}
	/**
	 * localhost:8080/wuyijiuchaoliufushiguan/adminRoleCategroy?reqName=addRoleCate
	 * **/
	
	public void addRoleCate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		
		DesignerDaoImpl deDao = new DesignerDaoImpl();//产品分类的实现类对象
		
		String name = request.getParameter("name");//接收前台传来的参数
		String position = request.getParameter("position");
		String img = request.getParameter("picName");
		if (name!=null && !name.equals("")) {
			List<Designer> deList = deDao.getDesignerByName(name);
			if (deList.size() == 0) {//没有查到，可以添加
				Designer de = new Designer(0, name, position, img);//根据接收的参数创建产品分类对象
				int d = deDao.addDesigner(de);
				if (d > 0) {
					out.print(Result.toClient(true, "添加成功", ""));
				} else {
					out.print(Result.toClient(false, "添加失败", ""));
				}
			} else {//查到了，不能添加
				out.print(Result.toClient(false, "设计师已存在", ""));
			}
		}
		
		
		out.flush();
		out.close();
	}
	/**
	 * 图片上传
	 * localhost:8080/wuyijiu/adminRoleCategroy?reqName=upload
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void upload(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();

		// 1.创建SmartUpload对象
		SmartUpload smart = new SmartUpload();
		// 2.设置编码格式
		smart.setCharset("UTF-8");
		// 3.初始化
		smart.initialize(getServletConfig(), request, response);

		try {// 正常
				// 4.上传
			smart.upload();
			// 5.设置上传文件的名字
			String fileName = System.currentTimeMillis() + ".jpg";
			// 6.将文件保存到指定目录
			smart.getFiles().getFile(0).saveAs("/upload/" + fileName);
			fileName = "upload/" + fileName;
			// 7.响应数据
			out.print(Result.toClient(true, "上传成功", fileName));
		} catch (SmartUploadException e) {// 异常
			e.printStackTrace();
			out.print(Result.toClient(false, "上传失败", "图片过大"));
		}
	}

	/**
	 * 编辑角色设计师 localhost:8080/wuyijiu/adminRoleCategroy?reqName=editRoleCate
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void editRoleCate(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();

		DesignerDaoImpl dDao = new DesignerDaoImpl();
		int did = Integer.parseInt(request.getParameter("deid"));
		Designer d = dDao.getDesignerById(did);

		Gson gson = new Gson();
		String res = gson.toJson(d);

		out.print(res);

		out.flush();
		out.close();

	}

	/**
	 * 修改角色设计师 localhost:8080/wuyijiu/adminRoleCategroy?reqName=updateRoleCate
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void updateRoleCate(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();

		DesignerDaoImpl urDao = new DesignerDaoImpl();

		int designerId = Integer.parseInt(request.getParameter("num"));
		String name = request.getParameter("dname");
		String position = request.getParameter("profession");
		String img = request.getParameter("picName");
		Designer ud = new Designer(designerId, name, position, img);
		System.out.println(ud);
		int n = urDao.updateDesigner(ud);

		if (n > 0) {
			out.print(Result.toClient(true, "修改成功", ""));
		} else {
			out.print(Result.toClient(false, "修改失败", ""));
		}

		out.flush();
		out.close();

	}
}
